package com.lanyuan.util;

import com.baidu.aip.imagesearch.AipImageSearch;
import com.baidu.aip.util.Base64Util;
import com.baidu.aip.util.Util;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class BaiduImageSearch {
//    public static final String APP_ID = "22773704";
//    public static final String API_KEY = "ScUrGavlxRQnS484mPYsoM9E";
//    public static final String SECRET_KEY = "aLMOQLXIngCPlWbFq9XmFpa3kjTESDrk";
    public static final String APP_ID = "22859586";
    public static final String API_KEY = "6tOfqBBwSktbym7rFFAGMbvV";
    public static final String SECRET_KEY = "q3bHbO5GpeTg1KuzxG6p2lq37BGdxQqF";
    public static void main(String[] args) {
        // 初始化一个AipImageSearch
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
//
//        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
//        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String path = "C:\\Users\\l\\Desktop\\t4.jpg";//"http://ntcbk.hlt360.com/uploadFile/tongue1.jpg";
        HashMap<String, String> options = new HashMap<String, String>();
//        options.put("tags", "100,11");
//        options.put("tag_logic", "0");
        options.put("pn", "0");
        options.put("rn", "5");
        JSONObject res = client.similarSearch(path,options);//client.similarSearchUrl(path, options);
        JSONArray result = res.getJSONArray("result");
        JSONObject object = (JSONObject) result.get(0);
        String brief = object.getString("brief");
        try {
            JSONObject object1 = new JSONObject(brief);
            System.out.println(object1.get("id")+"========");
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(brief);
//        System.out.println(brief.get("id")+"========");
        System.out.println(res.toString(2));

    }

    /*public void similarSearch(String url) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("tags", "100,11");
        options.put("tag_logic", "0");
        options.put("pn", "1");
        options.put("rn", "2");


        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);

        // 参数为本地路径
       *//* String image = "test.jpg";
        JSONObject res = client.similarSearch(image, options);
        System.out.println(res.toString(2));

        // 参数为二进制数组
        byte[] file = readFile("test.jpg");
        res = client.similarSearch(file, options);
        System.out.println(res.toString(2));*//*

        // 相似图检索—检索, 图片参数为远程url图片
        JSONObject res = client.similarSearchUrl(url, options);
        System.out.println(res.toString(2));

    }*/

    public static  Long similarSearch(String image) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        options.put("pn", "0");
        options.put("rn", "1");


        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);


        JSONObject res = client.similarSearch(Base64Util.decode(image),options);//client.similarSearchUrl(path, options);
        JSONArray result = res.getJSONArray("result");
        System.out.println(res);

        JSONObject object = (JSONObject) result.get(0);
        String brief = object.getString("brief");
        JSONObject object1 = new JSONObject(brief);
        System.out.println(object1.get("id")+"========");


        return object1.getLong("id");
    }

    public static void add(String path,String brief) throws Exception {
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();


        JSONObject res = client.similarAdd(Util.readFileByBytes(path), brief, options);

        System.out.println("搜索图片入库结果："+res.toString(2));
        if(StringUtils.isNotBlank(res.getString("error_code"))){
            throw new Exception(res.getString("error_msg"));
        }
       /* // 参数为二进制数组
        byte[] file = readFile("test.jpg");
        res = client.similarAdd(file, brief, options);
        System.out.println(res.toString(2));

        // 相似图检索—入库, 图片参数为远程url图片
        JSONObject res = client.similarAddUrl(url, brief, options);
        System.out.println(res.toString(2));
        */
    }
    public static void update(String path,String brief) throws IOException {
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        JSONObject res = client.similarUpdate(Util.readFileByBytes(path), options);
        System.out.println("搜索图片更新结果："+res.toString(2));


    }

    public static void del(String path,String brief) {
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        JSONObject res = client.similarDeleteByImage(path, options);
        System.out.println("搜索图片删除结果："+res.toString(2));

    }


}
